package kr.couponplay.android.activity.deal.task;

import java.io.InputStream;

import kr.couponplay.android.Constants;
import kr.couponplay.android.support.HttpUtils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class DealDetailImageLoader extends AsyncTask<String, Void, Bitmap> {
	int width;
	int height;
	
	public DealDetailImageLoader(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}
	
	@Override
	protected Bitmap doInBackground(String... params) {
		String thumbnailImageUrl = params[0];
//		Bitmap bmp = null;
		InputStream in = null;
		try {
			in = HttpUtils.executeToIn(thumbnailImageUrl);
//			bmp = resize(BitmapFactory.decodeStream(in));

			Bitmap bitmap = BitmapFactory.decodeStream(in);
			in.close();

//		    int viewHeight = 140;
//		    float width = bitmap.getWidth();
//		    float height = bitmap.getHeight();

		    // Calculate image's size by maintain the image's aspect ratio
//		    if(height > viewHeight) {
//		        float percente = (float)(height / 100);
//		        float scale = (float)(viewHeight / percente);
//		        width *= (scale / 100);
//		        height *= (scale / 100);
//		    }
		    // Resizing image
		    Bitmap sizingBmp = Bitmap.createScaledBitmap(bitmap, (int) width, (int) height, true);			
			return sizingBmp;
			
		} catch (Exception e) {
			Log.e(Constants.TAG, e.getMessage(), e);
			return null;
		}
	}
	
	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
	}
	
	@Override
	protected void onCancelled() {
		super.onCancelled();
	}
}  
